Completed
Pull Request — master (#41)
by Alejandro
03:20 queued 37s
created

ScrollToTop.js ➔ componentDidUpdate   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 7
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 1
CRAP Score 3.1852

Importance

Changes 0
Metric Value
cc 2
eloc 4
nc 2
dl 0
loc 7
ccs 1
cts 3
cp 0.3333
crap 3.1852
rs 10
c 0
b 0
f 0
nop 1
1
import React from 'react';
2
import { withRouter } from 'react-router-dom';
3 4
import PropTypes from 'prop-types';
4
5
export class ScrollToTopComponent extends React.Component {
6
  static propTypes = {
7
    location: PropTypes.object,
8
    window: PropTypes.shape({
9
      scrollTo: PropTypes.func,
10
    }),
11
    children: PropTypes.node,
12
  };
13
  static defaultProps = {
14
    window,
15
  };
16
17
  componentDidUpdate(prevProps) {
18
    const { location, window } = this.props;
19
20 2
    if (location !== prevProps.location) {
21
      window.scrollTo(0, 0);
22
    }
23
  }
24
25
  render() {
26
    return this.props.children;
27
  }
28
}
29
30
const ScrollToTop = withRouter(ScrollToTopComponent);
31
32
export default ScrollToTop;
33